// pages/rfid_binds/bind_labels/bind_labels.js
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
const mybehavior = require("../../../behaviors/requests")
Page({
  behaviors: [mybehavior],
  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    vehicle_id: "",
    choice: true,
    vehicle_type_1: "",
    vehicle_type_2: "",
    vehicle_type: "",
    rfid1: "",
    rfid2: "",
    hide: false,
    showPicker: false,
    showPicker1: false,
    columns: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    this.post("https://logisticsengineering.gree.com/rfid_api/rfid_manage/return_dic", {}).then(res => {
      console.log(res);
      that.setData({
        columns: res.data.type_value
      })
    }).catch((e) => {
      wx.showToast({
        title: String(e),
        icon: "none"
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  setShowFalse() {
    this.setData({
      showPicker: false
    })
  },
  setShowTrue() {
    this.setData({
      showPicker: true
    })
  },
  onClear() {
    Dialog.confirm({
      title: "数据重置",
      message: "确认托盘编号和RFID编号数据重置？",
    }).then(() => {
      this.vehicle_id = "";
      this.rfid1 = "";
      this.rfid2 = "";
      Toast.success("数据已清零");
    });
  },
  onConfirm(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    // Toast(`当前值：${value}, 当前索引：${index}`);
    this.setData({
      showPicker: false,
      vehicle_type_1: value
    })
  },

  onConfirm1(value) {
    this.unit_use = value;
    console.log("onConfirm", value);
    this.showPicker1 = false;
  },
  scan(event) {
    let type = event.target.dataset.s;
    const self = this;
    wx.scanCode().then((res) => {
      const hardware_code = res.result;
      if (type == "s") {
        self.setData({
          vehicle_id: hardware_code
        });
      } else if (type == "f1") {
        self.setData({
          rfid1: hardware_code
        });
      } else {
        self.setData({
          rfid2: hardware_code
        });
      }
    });
  },

  check() {
    request({
        url: "https://logisticsengineering.gree.com/injection_small/convert_vehicle_type",
        method: "post",
        params: {
          vehicle_id: this.vehicle_id,
        },
      })
      .then((res) => {
        if (res.code == 20000) {
          this.res = res;
          const data = res.data;
          Toast.success(res.msg);
          this.vehicle_type = data;
          //不展示选择器,展示显示框
          this.choice = false;
        } else {
          Toast.fail(res.msg);
          this.vehicle_id = '';
        }
      })
      .catch((e) => {
        Toast.fail(e);
      });
  },
  wechat_s() {
    wx.scanQRCode({
      needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
      scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
      success: (res) => {
        var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
        // window.location.href = result;//因为我这边是扫描后有个链接，然后跳转到该页面
        // $("#vehicle_id").val(result.split(",")[1]);
        if (result.includes(",")) {
          this.vehicle_id = result.split(",")[1];
        } else {
          this.vehicle_id = result;
        }
        this.check();
      },
      fail: (res) => {
        Toast.fail(JSON.stringify(res));
      },
    });
  },
  clear_type(event) {
    this.setData({
      checked: event.detail,
      vehicle_type_1: '',
      vehicle_type_2: '',
    })
  },
  onSubmit() {
    let department = wx.getStorageSync('department');
    let vehicle_type = "";
    if (this.data.vehicle_type_1 != '') {
      vehicle_type = this.data.vehicle_type_1
    }
    if (this.data.vehicle_type_2 != '') {
      vehicle_type = this.data.vehicle_type_2
    }
    if (department == "") {
      wx.showToast({
        title: '请使用邮箱登录绑定！',
        icon: "error"
      })
    } else if (vehicle_type == "") {
      wx.showToast({
        title: '类别不能为空',
        icon: "error"
      })
    } else if (this.data.rfid1 == "") {
      wx.showToast({
        title: 'rfid1不能为空',
        icon: "error"
      })
    } else if (this.data.rfid2 == "") {
      wx.showToast({
        title: 'rfid2不能为空',
        icon: "error"
      })
    } else {
      this.out(vehicle_type)
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  out(vehicle_type) {
    let that = this;
    let department = wx.getStorageSync('department');
    Dialog.confirm({
        title: "绑定确认",
        message: "托盘：" +
          this.data.vehicle_id +
          "\nRFID标签1:" +
          this.data.rfid1 +
          "\nRFID标签2:" +
          this.data.rfid2 +
          "\n托盘类别:" +
          vehicle_type +
          "\n使用单位：" +
          department
      })
      .then(() => {
        this.post("https://logisticsengineering.gree.com/rfid_api/rfid_manage/bind_double_labels", {
          vehicle_id: this.data.vehicle_id,
          rfid1: this.data.rfid1,
          rfid2: this.data.rfid2,
          vehicle_type: vehicle_type,
          responsible: department,
          species: '托盘'
        }).then(res => {
          if (res.code == 20000) {
            wx.showToast({
              title: res.msg,
            })
            that.setData({
              vehicle_id: "",
              rfid1: "",
              rfid2: ""
            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: "error"
            })
          }
        }).catch((e) => {
          wx.showToast({
            title: e.msg,
            icon: "none"
          })
        })
      })
      .catch((e) => {
        console.log(String(e))
      });
  },

  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})